package display;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JLabel;
import java.sql.SQLException;
import java.text.ParseException;

import database.Database;

import term.Term;
import Util.Util;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JComboBox;

public class Main {

	private JFrame frmStudentsFirst;
	private ToDoDialog todo;
	private InventoryQuery invQ;
	private InventoryUpdate invU;
	private InventoryData inv;
	private BookInfo bInfo;
	private CourseInfo cInfo;
	private AdoptionControl aCon;
	private JComboBox termBox;
	private JPanel panel;

	private static final String dbURL = "jdbc:derby://localhost:1527/inventory;create=true;user=me;password=mine";
	public static final String books = "books", pubs = "publishers",
			orders = "orders", courses = "courses", adoptions = "adoptions",
			quantities = "quantities";
	// jdbc Connection
	private static Database db = null;
	private MainActionListener mal = new MainActionListener();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
					Main window = new Main();
					window.frmStudentsFirst.setVisible(true);
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		
		Thread dbInit = new Thread("DB Initialization") {
			public void run() {
				initInv();
			}
		};

		//long t0 = System.currentTimeMillis();
		dbInit.start();
		
		initGUI();
		
		try {
			dbInit.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println("Starting time: "+ (System.currentTimeMillis() - t0));
		long t1 = System.currentTimeMillis();
		initTerms();
		Util.printStatusString("initTerms time: "+ (System.currentTimeMillis() - t1));
		
	}
	
	private void initGUI() {
		long t0 = System.currentTimeMillis();
		initDisplay();
		initButtons();
		Util.printStatusString("initGUI time: "+(System.currentTimeMillis()-t0));
	}
	
	private void initInv() {
		try {
			inv = new InventoryData(db = new Database(dbURL));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initDisplay() {
		frmStudentsFirst = new JFrame();
		frmStudentsFirst.setTitle("Students First");
		frmStudentsFirst.setBounds(100, 100, 480, 286);
		frmStudentsFirst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 0.0, 0.0, 0.0,
				1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 1.0, 0.0, 1.0, 0.0,
				0.0, 0.0, 1.0, Double.MIN_VALUE };
		frmStudentsFirst.getContentPane().setLayout(gridBagLayout);
		
		frmStudentsFirst.addWindowListener(mal);
	}

	private void initButtons() {
		
		JButton btnSales = new JButton("Sales");
		btnSales.setActionCommand("todo");
		btnSales.addActionListener(mal);

		JLabel lblHeader = new JLabel("Students First Inventory Management");
		GridBagConstraints gbc_lblHeader = new GridBagConstraints();
		gbc_lblHeader.gridwidth = 11;
		gbc_lblHeader.insets = new Insets(0, 0, 5, 0);
		gbc_lblHeader.gridx = 2;
		gbc_lblHeader.gridy = 0;
		frmStudentsFirst.getContentPane().add(lblHeader, gbc_lblHeader);
		
		GridBagConstraints gbc_btnSales = new GridBagConstraints();
		gbc_btnSales.insets = new Insets(0, 0, 5, 5);
		gbc_btnSales.gridx = 5;
		gbc_btnSales.gridy = 3;
		frmStudentsFirst.getContentPane().add(btnSales, gbc_btnSales);

		JButton btnInventory = new JButton("Inventory Query");
		btnInventory.setActionCommand("iq");
		btnInventory.addActionListener(mal);
		GridBagConstraints gbc_btnInventory = new GridBagConstraints();
		gbc_btnInventory.insets = new Insets(0, 0, 5, 5);
		gbc_btnInventory.gridx = 2;
		gbc_btnInventory.gridy = 5;
		frmStudentsFirst.getContentPane().add(btnInventory, gbc_btnInventory);

		JButton btnCourseLookup = new JButton("Course Info");
		btnCourseLookup.setActionCommand("ci");
		btnCourseLookup.addActionListener(mal);
		GridBagConstraints gbc_btnCourseLookup = new GridBagConstraints();
		gbc_btnCourseLookup.insets = new Insets(0, 0, 5, 5);
		gbc_btnCourseLookup.gridx = 9;
		gbc_btnCourseLookup.gridy = 5;
		frmStudentsFirst.getContentPane().add(btnCourseLookup,
				gbc_btnCourseLookup);

		JButton btnInventoryUpdate = new JButton("Inventory Update");
		btnInventoryUpdate.setActionCommand("iu");
		btnInventoryUpdate.addActionListener(mal);
		GridBagConstraints gbc_btnInventoryUpdate = new GridBagConstraints();
		gbc_btnInventoryUpdate.insets = new Insets(0, 0, 5, 5);
		gbc_btnInventoryUpdate.gridx = 2;
		gbc_btnInventoryUpdate.gridy = 6;
		frmStudentsFirst.getContentPane().add(btnInventoryUpdate,
				gbc_btnInventoryUpdate);

		JButton btnBookLooup = new JButton("Book Info");
		btnBookLooup.setActionCommand("bi");
		btnBookLooup.addActionListener(mal);
		GridBagConstraints gbc_btnBookLooup = new GridBagConstraints();
		gbc_btnBookLooup.insets = new Insets(0, 0, 5, 5);
		gbc_btnBookLooup.gridx = 9;
		gbc_btnBookLooup.gridy = 6;
		frmStudentsFirst.getContentPane().add(btnBookLooup, gbc_btnBookLooup);

		JButton btnAdoptions = new JButton("Adoptions");
		btnAdoptions.setActionCommand("ac");
		btnAdoptions.addActionListener(mal);

		JButton btnPurchaseOrders = new JButton("Purchase Orders");
		btnPurchaseOrders.setActionCommand("todo");
		btnPurchaseOrders.addActionListener(mal);
		
		GridBagConstraints gbc_btnPurchaseOrders = new GridBagConstraints();
		gbc_btnPurchaseOrders.insets = new Insets(0, 0, 5, 5);
		gbc_btnPurchaseOrders.gridx = 2;
		gbc_btnPurchaseOrders.gridy = 7;
		frmStudentsFirst.getContentPane().add(btnPurchaseOrders,
				gbc_btnPurchaseOrders);
		GridBagConstraints gbc_btnAdoptions = new GridBagConstraints();
		gbc_btnAdoptions.insets = new Insets(0, 0, 5, 5);
		gbc_btnAdoptions.gridx = 9;
		gbc_btnAdoptions.gridy = 7;
		frmStudentsFirst.getContentPane().add(btnAdoptions,
				gbc_btnAdoptions);
		
		panel = new JPanel();
		panel.addMouseListener(mal);

		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.insets = new Insets(0, 0, 0, 5);
		gbc_panel.gridx = 5;
		gbc_panel.gridy = 8;
		frmStudentsFirst.getContentPane().add(panel, gbc_panel);

	}
	
	private void initTerms() {
		termBox = new JComboBox();
		termBox.addItem("New Term");
		termBox.setActionCommand("tb");
		termBox.addActionListener(mal);
		termBox.addMouseListener(mal);
		termBox.setModel(new DefaultComboBoxModel(inv.initTerms()));
		termBox.setEditable(false);
		
		GridBagConstraints gbc_lblTerm = new GridBagConstraints();
		gbc_lblTerm.insets = new Insets(0, 0, 5, 5);
		gbc_lblTerm.gridx = 5;
		gbc_lblTerm.gridy = 1;
		frmStudentsFirst.getContentPane().add(termBox, gbc_lblTerm);
	}

	private void save() {
		long t0 = System.currentTimeMillis();
		inv.save();
		System.out.println("closing time: "+(System.currentTimeMillis()-t0));
	}
	
	private class MainActionListener implements ActionListener, MouseListener, WindowListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Util.printStatusString("action performed");
			String cmd = arg0.getActionCommand();
			if (cmd.equalsIgnoreCase("iq")) {
				invQ = new InventoryQuery(inv); invQ.setVisible(true);}
			else if (cmd.equalsIgnoreCase("iu")) {
				invU = new InventoryUpdate(); invU.setVisible(true);}
			else if (cmd.equalsIgnoreCase("bi")) {
				bInfo = new BookInfo(inv); bInfo.setVisible(true);}
			else if (cmd.equalsIgnoreCase("ci")) {
				cInfo = new CourseInfo(inv.getCurTerm()); cInfo.setVisible(true);}
			else if (cmd.equalsIgnoreCase("ac")) {
				aCon = new AdoptionControl(inv); aCon.setVisible(true);}
			else if (cmd.equalsIgnoreCase("tb")) {
				Util.printStatusString("term box!");
				String termString = termBox.getSelectedItem().toString();
				if (termString.equalsIgnoreCase("New Term"))
					new NewTermDialog(inv,db).setVisible(true);
				else
					inv.selectTerm(Term.parse(termString));
			}
			
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			if (arg0.getSource().equals(panel)) {
				DatabaseDisplay dd = new DatabaseDisplay(db);
				dd.setVisible(true);
			}
			else if (arg0.getSource().equals("tb")) {
				
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowActivated(WindowEvent arg0) {
			// TODO Auto-generated method stub
			termBox.setModel(new DefaultComboBoxModel(inv.getTerms().toArray()));
			termBox.addItem("New Term");
		}

		@Override
		public void windowClosed(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosing(WindowEvent arg0) {
			// TODO Auto-generated method stub
			save();
		}

		@Override
		public void windowDeactivated(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
